package com.example.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dellpc.myapplication.MyAdapter;
import com.example.dellpc.myapplication.R;
import com.example.dellpc.myapplication.RecyleActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 刘智强 on 2016/9/2.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    private List<RecyleActivity.Item> items;
    private Context context;
    private onItemClick onItemClick;
    public static final int TYPE_HEADER = 0;  //说明是带有Header的
    public static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    public static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的

    Map<Integer, Boolean> isChecked = new HashMap<>();

    List<Integer> ids = new ArrayList<>();//用来存储被选中的集合


    private View headerView;
    private View footerView;

    public View getHeaderView() {
        return headerView;
    }

    public void setHeaderView(View headerView) {
        this.headerView = headerView;
        notifyItemInserted(0);
    }

    public View getFooterView() {
        return footerView;
    }

    public void setFooterView(View footerView) {
        this.footerView = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    public onItemClick getOnItemClick() {
        return onItemClick;
    }

    public void setOnItemClick(onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }


    public RecyclerAdapter(Context context, List<RecyleActivity.Item> items) {
        this.items = items;
        this.context = context;
        if (items != null && items.size() > 0) {
            for (int i = 0; i < items.size(); i++) {
                isChecked.put(i, false);
            }
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (headerView != null && viewType == TYPE_HEADER) {
            return new ViewHolder(headerView);
        } else if (footerView != null && viewType == TYPE_FOOTER) {
            return new ViewHolder(footerView);
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_NORMAL) {
            Picasso.with(context).load(items.get(position - 1).getImage()).into(holder.iv);
            holder.tv_title.setText(items.get(position - 1).getTitle());
            if (position % 2 == 0) {
                holder.itemView.setBackground(context.getResources().getDrawable(R.drawable.selector_recyclerview_item));
            } else {
                holder.itemView.setBackground(context.getResources().getDrawable(R.drawable.selector_recyclerview_item1));
            }
            holder.checkBox.setTag(position);
//            holder.checkBox.setChecked(isChecked.get(position));
            if (ids != null) {
                holder.checkBox.setChecked(ids.contains(position) ? true : false);
            } else {
                holder.checkBox.setChecked(false);
            }

            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (isChecked) {
                        if (!ids.contains(holder.checkBox.getTag())) {
                            ids.add((Integer) (holder.checkBox.getTag()));

                        }

                    } else {
                        if (ids.contains(holder.checkBox.getTag())) {
                            ids.remove(holder.checkBox.getTag());
                        }


                    }

                }
            });
//            holder.checkBox.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (holder.checkBox.isChecked()) {
//                        isChecked.put(position, true);
//                    } else {
//                        isChecked.put(position, false);
//                    }
//                    for (int i = 0; i < items.size(); i++) {
//                        isChecked.put(i, false);
//                        if (i == position) {
//                            isChecked.put(i, true);
//                        }
//                    }
//                    notifyDataSetChanged();
//                }
//            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick.onClick(v, position);
                }
            });
        } else if (getItemViewType(position) == TYPE_HEADER) {
            return;

        } else if ((getItemViewType(position) == TYPE_FOOTER)) {
            return;

        }

    }

    @Override
    public int getItemCount() {
        if (headerView == null && footerView == null) {
            return items.size();
        } else if (headerView != null && footerView == null) {
            return items.size() + 1;
        } else if (headerView == null && headerView != null) {
            return items.size() + 1;
        }
        return items.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (headerView == null && footerView == null) {
            return TYPE_NORMAL;
        } else if (position == 0) {
            return TYPE_HEADER;
        } else if (position == getItemCount() - 1) {
            return TYPE_FOOTER;
        }

        return TYPE_NORMAL;
    }

    public void addItem(int position, RecyleActivity.Item item) {
        items.add(position, item);
        notifyItemInserted(position);
    }

    public void removeItem(int position) {
        items.remove(position);
        notifyItemRemoved(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv;
        private TextView tv_title;
        private CheckBox checkBox;


        public ViewHolder(View itemView) {
            super(itemView);

            if (itemView == headerView) {
                return;
            }

            if (itemView == footerView) {
                return;
            }
            iv = (ImageView) itemView.findViewById(R.id.iv_icon);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            checkBox = (CheckBox) itemView.findViewById(R.id.cb);
        }
    }

    public interface onItemClick {
        void onClick(View v, int position);

        void onLongClick();
    }

}
